package com.lanou.bookstore.order.dao;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.order.domain.Transit;
import com.lanou.bookstore.user.domain.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private ComboPooledDataSource dataSource = new ComboPooledDataSource();

    @Override
    public int insertOrder(Order order) {

        try {
            Connection conn = dataSource.getConnection();

            QueryRunner qr = new QueryRunner();

            int update = qr.update(conn,
                    "insert into orders (oid, ordertime, state," +
                            "total,uid)values (?,?,?,?,?)",
                    order.getOid(),order.getOrdertime(),
                    order.getState(),order.getTotal(),order.getUid());

            DbUtils.close(conn);
            return update;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void insertOrderItem(OrderItem item) {
        try {
            Connection conn = dataSource.getConnection();

            QueryRunner qr = new QueryRunner();

            int update = qr.update(conn,
                    "insert into orderitem (iid, count, subtotal," +
                            "oid,bid)values (?,?,?,?,?)",
                   item.getIid(), item.getCount(), item.getSubtotal(),
                    item.getOid(), item.getBid());

            DbUtils.close(conn);


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Order> queryAll(String uid) {
        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            List<Order> slist = qr.query(conn,
                    "select * from orders where uid = ? ",
                    new BeanListHandler<Order>(Order.class),uid
                    );

            DbUtils.close(conn);
            return slist;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Transit> queryTransit(String oid) {

        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            List<Transit> slist = qr.query(conn,
                    "select oid,count,subtotal,image,bname,author,price\n" +
                            "from bookstore.book join orderitem o on book.bid = o.bid\n" +
                            "where oid = ? ",
                    new BeanListHandler<Transit>(Transit.class),oid
            );

            DbUtils.close(conn);
            return slist;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int queryByOid(String oid) {
        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            Order order = qr.query(conn,
                    "select state from orders where oid = ? ",
                    new BeanHandler<Order>(Order.class), oid
            );

            DbUtils.close(conn);
            return order.getState();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 2;

    }

    @Override
    public List<Order> queryAll() {


        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            List<Order> orderList = qr.query(conn,
                    "select * from orders",
                    new BeanListHandler<Order>(Order.class)
            );

            DbUtils.close(conn);
            return orderList;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Transit> queryAll2() {

        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            List<Transit> slist = qr.query(conn,
                    "select oid,count,subtotal,image,bname,author,price\n" +
                            "from bookstore.book join orderitem o on book.bid = o.bid"
                            ,
                    new BeanListHandler<Transit>(Transit.class)
            );

            DbUtils.close(conn);
            return slist;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void changeOrderState(int state, String oid) {

        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            int update = qr.update(conn,
                    "update orders set state = ? where oid = ?"
                    , state, oid
            );
            System.out.println("更改订单状态结果:" + update);

            DbUtils.close(conn);


        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Order> queryByState(int state) {


        try {
            Connection conn = dataSource.getConnection();

            // 使用dbutils添加数据
            QueryRunner qr = new QueryRunner();
            List<Order> orderList = qr.query(conn,
                    "select * from orders where state = ?",
                    new BeanListHandler<Order>(Order.class),state
            );

            DbUtils.close(conn);
            return orderList;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


}
